package logic

import (
	"context"
	"fmt"

	"server-go/rpc/reward/internal/svc"
	"server-go/rpc/reward/internal/util"
	"server-go/rpc/reward/types/reward"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetCaptchaLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetCaptchaLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetCaptchaLogic {
	return &GetCaptchaLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetCaptchaLogic) GetCaptcha(in *reward.GetCaptchaInput) (*reward.GetCaptchaOutput, error) {
	out := &reward.GetCaptchaOutput{}
	// 验证是否为正确的邮箱
	if !util.VerifyEmailFormat(in.Email) {
		out.Status = 400
		out.Data = "请输入正确的邮箱号"
		return out, nil
	}
	randCode := util.RandCode(6)
	if err := util.RedisSet(l.svcCtx.Redis, in.Email, randCode, 60); err != nil {
		return out, err
	}
	data := fmt.Sprintf("您的验证码是 '%v'", randCode)
	out.Status = 200
	out.Data = data
	return out, nil
}
